<?php

class Model_Mapper_User {
	
	protected $_dbTable;
	private static $instance;
	
	public static function newInstance() {
		if (! self::$instance instanceof self) {
			self::$instance = new self ();
		}
		return self::$instance;
	}
	
	public function setDbTable($dbTable) {
		if (is_string ( $dbTable )) {
			$dbTable = new $dbTable ();
		}
		if (! $dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception ( 'Invalid table data gateway provided' );
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable ( 'Model_Table_User' );
		}
		return $this->_dbTable;
	}
	
	public function save(Model_User $user) {
		$data = array ('registration_date' => $user->getRegistration_date (), 'modified_date' => $user->getModified_date (), 'password' => $user->getPassword (), 'secret' => $user->getSecret (), 'email' => $user->getEmail (), 'enabled' => $user->getEnabled (), 'active' => $user->getActive (), 'pass_code' => $user->getPass_code (), 'pass_date' => $user->getPass_date (), 'pass_question' => $user->getPass_question (), 'pass_answer' => $user->getPass_answer (), 'pass_ip' => $user->getPass_ip (), 'permissions' => $user->getPermissions (), 'company' => $user->getCompany (), 'comments' => $user->getComments (), 'initial' => $user->getInitial (), 'first_name' => $user->getFirst_name (), 'middle_name' => $user->getMiddle_name (), 'last_name' => $user->getLast_name (), 'mobile' => $user->getMobile (), 'phone' => $user->getPhone (), 'info' => $user->getInfo (), 'website' => $user->getWebsite (), 'address_line_one' => $user->getAddress_line_one (), 'address_line_two' => $user->getAddress_line_two (), 'postcode' => $user->getPostcode (), 'city' => $user->getCity (), 'county' => $user->getCounty () );
		$key = $this->getDbTable ()->getPrimaryKey ();
		$method = 'get' . ucfirst ( $key );
		$val = $user->$method ();
		
		if ($val == null) {
			unset ( $data [$key] );
			$user->setUser_id($this->getDbTable ()->insert ( $data ));
		} else {
			$this->getDbTable ()->update ( $data, array ($key . ' = ?' => $val ) );
		}
	}
	
	public function getByEmail($email) {
		if ($email == '') {
			throw new Exception ( 'Empty Email provided.' );
		}
		
		$args = array ('email' => $email );
		
		$result = $this->getByCondition ( $args );
		if ($result) {
			return $result [0];
		}
		return array ();
	}
	
	public function getBySecret($secret) {
		if ($secret == '') {
			throw new Exception ( 'Empty Email provided.' );
		}
		
		$args = array ('secret' => $secret );
		
		$result = $this->getByCondition ( $args );
		if ($result) {
			return $result [0];
		}
		return array ();
	}
	
	public function checkLogin($email, $password) {
		$result = Model_Mapper_User::newInstance ()->getByEmail ( $email );
		
		if ($result) {
			
			// check active
			if (! $result->getActive ()) {
				return false;
			}
			if (! $result->getEnabled ()) {
				return false;
			}
			
			// user exists check for password
			if (strcmp ( sha1 ( $password ), $result->getPassword () ) === 0) {
				Session::newInstance ()->_set ( 'current_user', $result );
				return true;
			}
		} else {
			return false;
		}
	}
	
	/*
	 * *******************************
	 * GET CATEGORIES BY CONDITION
	 * *******************************
	 */
	
	function getByCondition(array $args, $array = false) {
		if (! is_array ( $args )) {
			throw new Exception ( 'Invalid Argument Array Provided' );
		}
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->from ( $table );
		foreach ( $args as $key => $value ) {
			$select->where ( $key . '=?', $value );
		}
		
		$result = $table->fetchAll ( $select );
		$result = $this->extend ( $result );
		if ($result) {
			if ($array) {
				foreach ( $result as $key => $item ) {
					$result [$key] = $item->toArray ();
				}
			}
			
			return $result;
		
		} else {
			return array ();
		}
	}
	
	private function extend($args) {
		$items = array ();
		foreach ( $args as $arg ) {
			$arg = $arg->toArray ();
			$item = new Model_User ( $arg );
			
			$items [] = $item;
		}
		return $items;
	}
	
	public function lastInsertID() {
		return $this->getDbTable ()->getAdapter ()->lastInsertId ();
	}
	
	public function beginTransaction() {
		$this->getDbTable ()->getAdapter ()->beginTransaction ();
	}
	
	public function commit() {
		$this->getDbTable ()->getAdapter ()->commit ();
	}
	
	public function rollBack() {
		$this->getDbTable ()->getAdapter ()->rollBack ();
	}
	
	public function getAll($enabled = false) {
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->from ( $this->getDbTable () );
		if ($enabled) {
			$select->where ( 'enabled=?', 1 );
		}
		
		return $this->extend ( $table->fetchAll ( $select ) );
	}
	
	public function getByID($id) {
		if ($id == '') {
			throw new Exception ( 'Invalid ID Provided.' );
		}
		$table = $this->getDbTable ();
		$select = $table->select ();
		$select->where ( $this->getDbTable ()->getPrimaryKey () . '=?', $id );
		$select->from ( $this->getDbTable () );
		$result = $this->extend ( $table->fetchAll ( $select ) );
		return $result [0];
	}

}